package com.hp.btc.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hp.btc.bean.BeanAudio;
import com.hp.btc.bean.BeanCampaign;
import com.hp.btc.bean.BeanDescriptionVDN;
import com.hp.btc.bean.BeanServiceVDN;
import com.hp.btc.bean.BeanVDNResponse;

@Service("desService")
public class DescriptionVDNImpl implements DescriptionVDNService{
	
	private static final Logger logger = LoggerFactory.getLogger(DescriptionVDNImpl.class);
	
	private static List<BeanDescriptionVDN> listVDN = null;
	private static List<BeanCampaign> listCampaing = null;
	private static List<BeanCampaign> listCampaingAux = null;
	
	private List<BeanAudio>audios_message_error;
	
	enum ServiceHour { DAY, START_HOUR, END_HOUR }	
	
	public static List<BeanCampaign> getListCampaingAux() {
		return listCampaingAux;
	}

	public static void setListCampaingAux(List<BeanCampaign> listCampaingAux) {
		DescriptionVDNImpl.listCampaingAux = listCampaingAux;
	}

	public static List<BeanCampaign> getListCampaing() {
		return listCampaing;
	}
	
	public List<BeanAudio> getAudios_message_error() {
		return audios_message_error;
	}

	public void setAudios_message_error(List<BeanAudio> audios_message_error) {
		this.audios_message_error = audios_message_error;
	}

	public List<BeanDescriptionVDN> getAllDescriptionVdns() {
		return listVDN;
	}
	
	public void putDescripctionVDNs(List<BeanDescriptionVDN> listDescriptionVdn) {
		//logger.info("{}", listDescriptionVdn);
		listVDN = new ArrayList<BeanDescriptionVDN>();
		if(listDescriptionVdn != null){
			listVDN.addAll(listDescriptionVdn);
		}
	}

	public BeanDescriptionVDN getDescriptionById(String id) {
		
		for(BeanDescriptionVDN bean : listVDN){
			if(bean.getIdDescription().equalsIgnoreCase(id)){
				logger.info("{}",bean);
					return bean;
			}
		}
		return null;
	}
	
	public void addServiceInDescriptionList(String idDescription, BeanServiceVDN serviceVdn){
		logger.info("addServiceInDescriptionList  idDescription="+idDescription, "  serviceVdn="+serviceVdn);
		List<BeanServiceVDN> services = getDescriptionById(idDescription).getOptionService();
		int newId = services.size()+1;
		serviceVdn.setId(""+newId);
		if(org.springframework.util.StringUtils.hasText(serviceVdn.getAudioNameServHours())){
			if(serviceVdn.getAudioNameServHours().equals("0"))
				serviceVdn.setAudioNameServHours("");
		}
		if(org.springframework.util.StringUtils.hasText(serviceVdn.getAudioNameOutServHours())){
			if(serviceVdn.getAudioNameOutServHours().equals("0"))
				serviceVdn.setAudioNameOutServHours("");
		}
		logger.debug("new service {}", serviceVdn);
		services.add(serviceVdn);		
	}
	
	public void updateServiceInDescription(BeanDescriptionVDN beanDescription, BeanServiceVDN serviceVdn){
		logger.info("updateServiceInDescription options="+beanDescription.getOption() + "  serviceVdn="+serviceVdn);
		List<BeanDescriptionVDN>descriptions = getDescriptionVdnsForNameAndShortCode(beanDescription.getCampaign(), 
					beanDescription.getShortcodeDescription());
		BeanDescriptionVDN descriptionVDN = null;
		for (BeanDescriptionVDN beanDescriptionVDN : descriptions) {
			if(beanDescriptionVDN.getOption().equalsIgnoreCase(beanDescription.getOption())){
				descriptionVDN = beanDescriptionVDN;
				break;
			}
		}
		if(org.springframework.util.StringUtils.hasText(serviceVdn.getAudioNameServHours())){
			if(serviceVdn.getAudioNameServHours().equals("0"))
				serviceVdn.setAudioNameServHours("");
		}
		if(org.springframework.util.StringUtils.hasText(serviceVdn.getAudioNameOutServHours())){
			if(serviceVdn.getAudioNameOutServHours().equals("0"))
				serviceVdn.setAudioNameOutServHours("");
		}
		List<BeanServiceVDN> services = descriptionVDN.getOptionService();
		List<BeanServiceVDN> servicesTmp = new ArrayList<BeanServiceVDN>();
		for (BeanServiceVDN beanServiceVDN : services) {
			if(beanServiceVDN.getId().equalsIgnoreCase(serviceVdn.getId()))
				servicesTmp.add(serviceVdn);
			else
				servicesTmp.add(beanServiceVDN);
		}
		services.clear();
		services.addAll(servicesTmp);
	}
	
	public BeanDescriptionVDN getDescriptionByCampaignAndOption(String campaign,String option, List<BeanDescriptionVDN> listVDNCurrent) {
		logger.info("getDescriptionByCampaignAndOption campaign="+campaign+", option="+option);
		for(BeanDescriptionVDN bean : listVDNCurrent){
			if(bean.getCampaign().equalsIgnoreCase(campaign) && bean.getOption().equalsIgnoreCase(option)){
				logger.info("{}",bean);
					return bean;
			}
		}
		return null;
	}
	
	
	public BeanDescriptionVDN getDescriptionByCampaignAndOptionAux(String campaignName,String option, String labelShortCode) {
	
		if(listCampaingAux != null) {
			for(BeanCampaign campaing:listCampaingAux) {
				
				if(labelShortCode.equals(campaing.getLabelShortCode())  && campaignName.equals(campaing.getName())) {
					
					for(BeanDescriptionVDN description: campaing.getListBeanDescriptionVDN()) {
						
						if(campaignName.equals(description.getCampaign()) &&  option.equals(description.getOption())) {
							
							return description;
						}
					}		
				}
			}
		}
		
		return null;
	}
	
	
	public void copyListCampaign() {
		 
		try {
			
			if(listCampaing != null) {
				listCampaingAux = new ArrayList<BeanCampaign>();
				List<BeanDescriptionVDN> listBeanDescriptionVDN = new  ArrayList<BeanDescriptionVDN>();
				
				
				for(BeanCampaign campaing: listCampaing) {
					
					BeanCampaign campaingAux = new BeanCampaign();
					
					campaingAux.setId(campaing.getId());
					campaingAux.setIdshortcode(campaing.getIdshortcode());
					campaingAux.setName(campaing.getName());
					campaingAux.setLabelShortCode(campaing.getLabelShortCode());
					campaingAux.setStatus(campaing.getStatus());
					campaingAux.setFilePath(campaing.getFilePath());
					campaingAux.setShortCode(campaing.getShortCode());
					campaingAux.setDefaultcampaign(campaing.getDefaultcampaign());
					
					List<BeanDescriptionVDN> listBeanDescription = campaing.getListBeanDescriptionVDN();
					
					for(BeanDescriptionVDN description: listBeanDescription) {
						
						BeanDescriptionVDN descriptionAux = new BeanDescriptionVDN();
						
						descriptionAux.setIdDescription(description.getIdDescription());
						descriptionAux.setCampaign(description.getCampaign());
						descriptionAux.setSkill(description.getSkill());
						descriptionAux.setOption(description.getOption());
						descriptionAux.setMessageError(description.getMessageError());
						descriptionAux.setVdnDefault(description.getVdnDefault());
						descriptionAux.setAudioDefault(description.getAudioDefault());
						descriptionAux.setLastUserModified(description.getLastUserModified());
						descriptionAux.setLastDateModified(description.getLastDateModified());
						descriptionAux.setHaveMessageError(description.isHaveMessageError());
						descriptionAux.setBeanVdnDefault(description.getBeanVdnDefault());
						descriptionAux.setIndexPosition(description.getIndexPosition());
						descriptionAux.setWeigth(description.getWeigth());
						descriptionAux.setCount(description.getCount());
						descriptionAux.setMessageOption(description.getMessageOption());
						descriptionAux.setShortcodeDescription(description.getShortcodeDescription());
						
						if(description.getCurrentOptionService()!=null){
							List<BeanServiceVDN> currentOptionServiceAux = new ArrayList<BeanServiceVDN>();			
							
							
							for(BeanServiceVDN serviceVDN : description.getCurrentOptionService()) {
								BeanServiceVDN serviceVDNAux = new BeanServiceVDN();
								
								serviceVDNAux.setId(serviceVDN.getId());
								serviceVDNAux.setIdDescription(serviceVDN.getIdDescription());
								serviceVDNAux.setServiceHour(serviceVDN.getServiceHour());
								serviceVDNAux.setVdn(serviceVDN.getVdn());
								serviceVDNAux.setVdnOutHour(serviceVDN.getVdnOutHour());
								serviceVDNAux.setRate(serviceVDN.getRate());
								serviceVDNAux.setStatus(serviceVDN.getStatus());
								serviceVDNAux.setCount(serviceVDN.getCount());
								serviceVDNAux.setBeanVDN(serviceVDN.getBeanVDN());
								serviceVDNAux.setBeanVDNOutHour(serviceVDN.getBeanVDNOutHour());
								serviceVDNAux.setSendTextToAudit(serviceVDN.getSendTextToAudit());
								serviceVDNAux.setAudioNameOutServHours(serviceVDN.getAudioNameOutServHours());
								serviceVDNAux.setAudioNameServHours(serviceVDN.getAudioNameServHours());
								serviceVDNAux.setOutHour(serviceVDN.getOutHour());
								currentOptionServiceAux.add(serviceVDNAux);
								
							}
							
							descriptionAux.setCurrentOptionService(currentOptionServiceAux);
						}else {
							descriptionAux.setCurrentOptionService(null);
						}
						
						listBeanDescriptionVDN.add(descriptionAux);
					}
					
					
					campaingAux.setListBeanDescriptionVDN(listBeanDescriptionVDN);
					
					listCampaingAux.add(campaingAux);
				}
			}
		} catch(Exception e){
			e.printStackTrace();
		}
	 }
	
	
	public void putListCampaing(List<BeanCampaign> listCampaing) {
	
		copyListCampaign();
		DescriptionVDNImpl.listCampaing = listCampaing;
	}
	
	public void putCurrentDescriptionVDNs(List<BeanDescriptionVDN> listDescriptionVdn) {
		
		List<BeanServiceVDN> listCurrentServiceVDN = new ArrayList<BeanServiceVDN>();
				
		for(BeanDescriptionVDN beanDescription : listDescriptionVdn){
			
			try {
			
				listCurrentServiceVDN = loadOptionServiceCurrentDay(beanDescription.getOptionService());
				
				if(listCurrentServiceVDN.size()>0) {
					
					Integer weigth = summarizeRate(listCurrentServiceVDN);
					
					beanDescription.setWeigth(weigth);
					beanDescription.setCurrentOptionService(listCurrentServiceVDN);
					
				}else {
					beanDescription.setCurrentOptionService(null);
				}
				
				for(BeanCampaign shortcodes:beanDescription.getShortcodesCampaign()) {
					for(BeanCampaign campaing:listCampaing) {
						if(campaing.getId().equals(shortcodes.getId())){
							
							
							BeanDescriptionVDN beanDescriptionAux = new BeanDescriptionVDN();
							
							beanDescriptionAux.setIdDescription(beanDescription.getIdDescription());
							beanDescriptionAux.setCampaign(beanDescription.getCampaign());
							beanDescriptionAux.setSkill(beanDescription.getSkill());
							beanDescriptionAux.setOption(beanDescription.getOption());
							beanDescriptionAux.setMessageError(beanDescription.getMessageError());
							beanDescriptionAux.setVdnDefault(beanDescription.getVdnDefault());
							beanDescriptionAux.setLastUserModified(beanDescription.getLastUserModified());
							beanDescriptionAux.setLastDateModified(beanDescription.getLastDateModified());
							beanDescriptionAux.setHaveMessageError(beanDescription.isHaveMessageError());
							beanDescriptionAux.setBeanVdnDefault(beanDescription.getBeanVdnDefault());
							beanDescriptionAux.setOptionService(beanDescription.getOptionService());
							beanDescriptionAux.setWeigth(beanDescription.getWeigth());
							beanDescriptionAux.setShortcodesCampaign(beanDescription.getShortcodesCampaign());
							beanDescriptionAux.setMessageOption(beanDescription.getMessageOption());
							beanDescriptionAux.setShortcodeDescription(beanDescription.getShortcodeDescription());
							beanDescriptionAux.setAudio(beanDescription.getAudio());
						
							BeanDescriptionVDN beanDescriptionVdn = getDescriptionByCampaignAndOptionAux(campaing.getName(),beanDescriptionAux.getOption(),campaing.getLabelShortCode());
							
							if(beanDescriptionVdn != null) {
								beanDescriptionAux.setCount(beanDescriptionVdn.getCount());
							} else {
								beanDescriptionAux.setCount(beanDescription.getCount());
							}
							if(beanDescription.getCurrentOptionService()!=null){
								List<BeanServiceVDN> currentOptionServiceAux = new ArrayList<BeanServiceVDN>();			
								
								
								List<BeanServiceVDN> listVDNServiceCampaingAux = null; 
									
								if(beanDescriptionVdn != null) {
									listVDNServiceCampaingAux = beanDescriptionVdn.getCurrentOptionService(); 
								}
								
								for(BeanServiceVDN o : beanDescription.getCurrentOptionService()) {
									BeanServiceVDN p = new BeanServiceVDN();
									
									p.setId(o.getId());
									p.setIdDescription(o.getIdDescription());
									p.setServiceHour(o.getServiceHour());
									p.setVdn(o.getVdn());
									p.setVdnOutHour(o.getVdnOutHour());
									p.setRate(o.getRate());
									p.setStatus(o.getStatus());
									p.setBeanVDN(o.getBeanVDN());
									p.setBeanVDNOutHour(o.getBeanVDNOutHour());
									p.setSendTextToAudit(o.getSendTextToAudit());
									p.setAudioNameOutServHours(o.getAudioNameOutServHours());
									p.setAudioNameServHours(o.getAudioNameServHours());
									p.setOutHour(o.getOutHour());
									
									if(listCampaingAux!=null){
										
										if(listVDNServiceCampaingAux!=null){	
											for(BeanServiceVDN serviceVDNAux : listVDNServiceCampaingAux) {
											
												if(serviceVDNAux.getId().equals(p.getId())) {
													p.setCount(serviceVDNAux.getCount());
													break;
												}
											}
										} else {
											p.setCount(o.getCount());
										}
									} else {
										p.setCount(o.getCount());
									}
									
									currentOptionServiceAux.add(p);
								}
								
								beanDescriptionAux.setCurrentOptionService(currentOptionServiceAux);
		
								boolean isVdnEquals = isEqualsVdns(listVDNServiceCampaingAux,beanDescription.getCurrentOptionService());
								
								if(isVdnEquals == true) {
									beanDescriptionAux.setIndexPosition(beanDescriptionVdn.getIndexPosition());
								} else {
									resetVDNList(beanDescriptionAux);
								}
								
							}else {
								beanDescriptionAux.setCurrentOptionService(null);
							}
							
							campaing.getListBeanDescriptionVDN().add(beanDescriptionAux);
							break;
						}
					}
				}
			} catch(Exception e) {
				logger.error("", e);
			}
		}			
	}
	
	public boolean isEqualsVdns(List<BeanServiceVDN> listNew, List<BeanServiceVDN> listAux) {
		
		Integer counterVdn = 0;
		
		if(listNew != null && listAux != null) {
			
			if(listNew.size() == listAux.size()) {
				
				for(BeanServiceVDN vdnNew:  listNew) {
					for(BeanServiceVDN vdnAux:  listAux) {
						if(vdnNew.getId().equals(vdnAux.getId()) && vdnNew.getRate().equals(vdnAux.getRate())) {
							counterVdn++;
						}
					}
				}
				
				if(counterVdn.equals(listNew.size())) {
					return true;
				}
			}
		}
		
		return false;
		
	}
	
	public Integer summarizeRate(List<BeanServiceVDN> listOptionServiceCurrentDay) {
		
		Integer weigth = 0;
		
		for(BeanServiceVDN bean : listOptionServiceCurrentDay) {	
			weigth += bean.getRate();
		}
		
		return  weigth;
	}
	
	public List<BeanServiceVDN> loadOptionServiceCurrentDay(List<BeanServiceVDN> serviceVdn) throws Exception  {
		
		List<BeanServiceVDN> listOptionServiceCurrentDay = new ArrayList<BeanServiceVDN>();

		Calendar currentDate = new GregorianCalendar();
		
		Integer hour = currentDate.get(Calendar.HOUR_OF_DAY);
		Integer currentday = currentDate.get(Calendar.DAY_OF_WEEK);
		
		for(BeanServiceVDN optionService : serviceVdn) {
			boolean outHour = false;
			if(optionService.getStatus().equals("enabled")) {
				
				String[] vdnServiceHour = optionService.getServiceHour().split("\\|");

				
				for(String vdnServiceHourDate : vdnServiceHour) {
					Integer day = getDateVdnServiceHour(vdnServiceHourDate, "DAY");
					
					if(currentday==day) {
						
						Integer startHour = getDateVdnServiceHour(vdnServiceHourDate, "START_HOUR");
						Integer endHour = getDateVdnServiceHour(vdnServiceHourDate, "END_HOUR");
						
						if(hour >= startHour && hour <= endHour) {
							listOptionServiceCurrentDay.add(optionService);	
							break;
						} else {
							outHour = true;
						}
					}
				}
				
				if(outHour == true) {
					if(optionService.getVdnOutHour()!=null) {
						if(!"".equals(optionService.getVdnOutHour()) && !"0".equals(optionService.getVdnOutHour())) {
							optionService.setOutHour(true);
							listOptionServiceCurrentDay.add(optionService);
						}
					}
				}
			}
		}
		
		Collections.sort(listOptionServiceCurrentDay);
		
		return listOptionServiceCurrentDay;
	}
	
	public Integer getDateVdnServiceHour(String optionService,String serviceHour) {

		Calendar currentDate = new GregorianCalendar();
		Integer date = currentDate.get(Calendar.DAY_OF_WEEK);
		
		if(optionService.length()==5) {
			if(ServiceHour.DAY.toString().equals(serviceHour)){
				date = Integer.parseInt(optionService.substring(0, 1));
			}else if(ServiceHour.START_HOUR.toString().equals(serviceHour)) {
				date = Integer.parseInt(optionService.substring(1, 3));
			}else if(ServiceHour.END_HOUR.toString().equals(serviceHour)) {
				date = Integer.parseInt(optionService.substring(3, 5));
			}
		}
		
		return date;
	}
	
	private BeanCampaign findCampaingById(String id) {
		
		for(BeanCampaign searchCampaing : listCampaing){
			if(searchCampaing.getId().equals(id)) {
				return searchCampaing;
			}
		}
		return null;
	}
	
	private List<BeanCampaign> findCampaingByName(String name) {
		
		List<BeanCampaign> listAux = new ArrayList<BeanCampaign>();
		
		for(BeanCampaign searchCampaing : listCampaing){
			if(searchCampaing.getName().equals(name)) {
				listAux.add(searchCampaing);
			}
		}
		
		return listAux;
	}
	
	private String getVdn(BeanDescriptionVDN beanDescription) {
		String vdn = "";
	
		if(beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getOutHour() == true) {
			if(beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getVdnOutHour() != null) {
				vdn = beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getVdnOutHour();
			}
		} else {
			if(beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getVdn() != null) {
				vdn = beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getVdn();
			}
		}
		return vdn;
	}
	
	private String getAudio(BeanDescriptionVDN beanDescription) {
		String audio = "";
		
		if(beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getOutHour() == true) {
			if(beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getAudioNameOutServHours() != null) {
				audio = beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getAudioNameOutServHours();
			}
		} else {
			if(beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getAudioNameServHours() != null) {
				audio = beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getAudioNameServHours();
			}
		}
		return audio;
	}
	
	public synchronized BeanVDNResponse getTransferVdn(String campaing,String option,String calledNumber) {
		
		BeanVDNResponse response = new BeanVDNResponse();
		BeanDescriptionVDN beanDescription = null;
		
		try {
			
			Boolean stopSearch = false;
			Boolean findShortCode = false;
			
		    List<BeanCampaign> listCampaingFiltered = findCampaingByName(campaing);
			
			for(BeanCampaign campaingSearch:listCampaingFiltered){
				
				String shorcodes[] = campaingSearch.getShortCode().split("\\|");
				
				for(String shortcode:shorcodes) {
					if(calledNumber.equals(shortcode)) {
						beanDescription = getDescriptionByCampaignAndOption(campaingSearch.getName(), option,campaingSearch.getListBeanDescriptionVDN());
						stopSearch = true;
						findShortCode = true;
						break;
					}
				}
				
				if(stopSearch) {
					stopSearch=false;
					break;
				}
			}
			
			if(findShortCode == false){
				for(BeanCampaign campaingSearch:listCampaingFiltered){
					if(!campaingSearch.getDefaultcampaign().equals("")){
						BeanCampaign defaultCampaing = findCampaingById(campaingSearch.getDefaultcampaign());
						beanDescription = getDescriptionByCampaignAndOption(defaultCampaing.getName(), option,defaultCampaing.getListBeanDescriptionVDN());
						stopSearch=true;
						break;
					}
				}
			} 		
			
			if(beanDescription != null) {	
				if(beanDescription.getCurrentOptionService()!=null) {
					if(beanDescription.getCurrentOptionService().size() > 1) {
							
						if(beanDescription.getCount() >= beanDescription.getWeigth()) {
							resetVDNList(beanDescription);
							beanDescription.setIndexPosition(0);
						}
						
						if(beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getRate() > beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getCount()) {
							
							String vdn = getVdn(beanDescription);
							String audio = getAudio(beanDescription);
										
							response.setVDNResponse("0", "OK", vdn, audio);
							
							int count = beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getCount()+1;
							
							beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).setCount(count);
						
						    if (beanDescription.getIndexPosition() +1 >= beanDescription.getCurrentOptionService().size()){ 
						    	beanDescription.setIndexPosition(0);
						    }else{
						    	beanDescription.setIndexPosition(beanDescription.getIndexPosition()+1);
						    }
											
						} else {
							
							beanDescription.setIndexPosition(0);
							
							if (beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getRate() > beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getCount()){	
								
								String vdn = getVdn(beanDescription);
								String audio = getAudio(beanDescription);
								
								response.setVDNResponse("0", "OK", vdn, audio);
								
								int count = beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).getCount()+1;
								beanDescription.getCurrentOptionService().get(beanDescription.getIndexPosition()).setCount(count);
						
								beanDescription.setIndexPosition(beanDescription.getIndexPosition()+1);
							}	
						}
						beanDescription.setCount(beanDescription.getCount()+1);
					} else {

						String vdn = "";
						String audio = "";
						
						if(beanDescription.getCurrentOptionService().get(0).getOutHour() == true) {
							if(beanDescription.getCurrentOptionService().get(0).getVdnOutHour()!=null) {
								vdn = beanDescription.getCurrentOptionService().get(0).getVdnOutHour();
							}
							
							if(beanDescription.getCurrentOptionService().get(0).getAudioNameOutServHours()!=null) {
								audio = beanDescription.getCurrentOptionService().get(0).getAudioNameOutServHours();
							}
							
						} else {
							if(beanDescription.getCurrentOptionService().get(0).getVdn()!=null) {
								vdn = beanDescription.getCurrentOptionService().get(0).getVdn();
							}
							
							if(beanDescription.getCurrentOptionService().get(0).getAudioNameServHours()!=null) {
								audio = beanDescription.getCurrentOptionService().get(0).getAudioNameServHours();
							}
						}
						
						response.setVDNResponse("0", "OK", vdn,audio);
					}
				} else {
					if(beanDescription.getVdnDefault() != null) {
						if(!"".equals(beanDescription.getVdnDefault())) {
							
							response.setVDNResponse("0", "OK" , beanDescription.getVdnDefault(),beanDescription.getAudioDefault());
						} else {
							response.setVDNResponse("E01", beanDescription.getMessageError() ,"","");
						}
					} else {
						response.setVDNResponse("E01", beanDescription.getMessageError() ,"","");
					}
				}
			} else {
				response.setVDNResponse("E02", "No find vdn with campaing = "+campaing +" and option = "+option, "","");
			}
			
		} catch(Exception e) {
			response.setVDNResponse("E03", "Excpetion in server the message is :"+e.getMessage(), "","");
		}
		
		return response;
	}

	public void resetVDNList(BeanDescriptionVDN beanDescription) throws Exception {
		  
		beanDescription.setCount(0);
		beanDescription.setIndexPosition(0);
		for(BeanServiceVDN beanService : beanDescription.getCurrentOptionService()) {
			beanService.setCount(0);
		}	  
	}

	public final List<BeanDescriptionVDN> getDescriptionVdnsForName(String campagin) {
		List<BeanDescriptionVDN> descritionTmp = new ArrayList<BeanDescriptionVDN>();
		for (BeanDescriptionVDN descriptionVDN : listVDN) {
			if(descriptionVDN.getCampaign().equalsIgnoreCase(campagin))
				descritionTmp.add(descriptionVDN);
		}
		return descritionTmp;
	}

	public List<BeanDescriptionVDN> getDescriptionVdnsForNameAndShortCode(String nameCampaign, String shortCode) {
		List<BeanDescriptionVDN> descriptionTmp = new ArrayList<BeanDescriptionVDN>();
		List<BeanDescriptionVDN> listDescriptionVDN = getDescriptionVdnsForName(nameCampaign);
		List<BeanCampaign> campaignTmp = null;
		for (BeanDescriptionVDN descriptionVDN : listDescriptionVDN) {
			campaignTmp = new ArrayList<BeanCampaign>();
			for(BeanCampaign campaign : descriptionVDN.getShortcodesCampaign()){
				if(campaign.getId().equalsIgnoreCase(shortCode) || campaign.getLabelShortCode().equalsIgnoreCase(shortCode)){
					descriptionVDN.setShortcodeDescription(campaign.getLabelShortCode());
				}
				campaignTmp.add(campaign);
			}
			if(!campaignTmp.isEmpty()){
				descriptionVDN.setShortcodesCampaign(campaignTmp);
				descriptionTmp.add(descriptionVDN);
			}
		}
		return descriptionTmp;
	}

	public List<BeanCampaign> getAllVDNCurrent() {
		return listCampaing;
	}

	@Override
	public void resetCounter() {
		
		for(BeanCampaign campaing:listCampaing) {
			for (BeanDescriptionVDN descriptionVDN : campaing.getListBeanDescriptionVDN()) {
				descriptionVDN.setCount(0);
				descriptionVDN.setIndexPosition(0);
				for (BeanServiceVDN serviceVDN : descriptionVDN.getCurrentOptionService()) {
					serviceVDN.setCount(0);
				}	
			}		
		}
	}
}
